﻿using System;
using StockAccessors.Interdin.Model;
using StockModel;
using Utils;

namespace StockAccessors.Interdin.Engine
{
	internal class FuturesPageReader : PageReader
	{
		#region Attributes

        private AccessorGroup _meffFutures;
        private AccessorGroup _eurexFutures;
        private AccessorGroup _otherIndexFutures;
        private AccessorGroup _energyAndMaterialsFutures;
		private AccessorGroup _currencyFutures;
		
        private FuturesWebPage _page;

		#endregion

		#region Properties

		public FuturesWebPage Page
		{
			get
			{
				return _page;
			}
			set
			{
				if (_page != value)
				{
					_page = value;
					base.OnPropertyChanged("Page");
				}
			}
		}

        public AccessorGroup MEFFFutures
		{
			get 
            {
                lock (base.ListLock)
                {
                    return _meffFutures;
                }
            }
            private set
            {
                lock (base.ListLock)
                {
					if (_meffFutures != value)
					{
						_meffFutures = value;
						base.OnPropertyChanged("MEFFFutures");
					}
                }
            }
		}

        public AccessorGroup EUREXFutures
		{
            get 
            {
                lock (base.ListLock)
                {
                	return _eurexFutures;
                }
            }
            private set
            {
                lock (base.ListLock)
                {
					if (_eurexFutures != value)
					{
						_eurexFutures = value;
						base.OnPropertyChanged("EUREXFutures");
					}
                }
            }
		}

        public AccessorGroup OtherIndexFutures
		{
            get 
            {
                lock (base.ListLock)
                {
                    return _otherIndexFutures;
                }
            }
            private set
            {
                lock (base.ListLock)
                {
					if (_otherIndexFutures != value)
					{
						_otherIndexFutures = value;
						base.OnPropertyChanged("OtherIndexFutures");
					}
                }
            }
		}

        public AccessorGroup EnergyAndMaterialsFutures
		{
            get 
            {
                lock (base.ListLock)
                {
                    return _energyAndMaterialsFutures;
                }
            }
            private set
            {
                lock (base.ListLock)
                {
					if (_energyAndMaterialsFutures != value)
					{
						_energyAndMaterialsFutures = value;
						base.OnPropertyChanged("EnergyAndMaterialsFutures");
					}
                }
            }
		}

        public AccessorGroup CurrencyFutures
		{
            get 
            {
                lock (base.ListLock)
                {
                    return _currencyFutures;
                }
            }
            private set
            {
                lock (base.ListLock)
                {
					if (_currencyFutures != value)
					{
						_currencyFutures = value;
						base.OnPropertyChanged("CurrencyFutures");
					}
                }
            }
		}

		#endregion

		#region Public Methods

		public FuturesPageReader() : base(Properties.Settings.Default.RefreshRate)
		{
 		}

		public void ReadOnce()
		{
			this.Page = new FuturesWebPage(base.ReadPageContents(string.Format(@"https://www.interdinfuturos.com/Broker/Futuros.asp?nc={0}", base.ContractNumber)));

			this.MEFFFutures = new AccessorGroup(StockGroupName.MEFFFutures, base.Converter.ConvertList(_page.MeffGroup.Futures));
			this.EUREXFutures = new AccessorGroup(StockGroupName.EUREXFutures, base.Converter.ConvertList(_page.EurexGroup.Futures));
			this.OtherIndexFutures = new AccessorGroup(StockGroupName.OtherIndexFutures, base.Converter.ConvertList(_page.OtherIndexGroup.Futures));
			this.EnergyAndMaterialsFutures = new AccessorGroup(StockGroupName.EnergyAndMaterialsFutures, base.Converter.ConvertList(_page.EnergyAndMaterialsGroup.Futures));
			this.CurrencyFutures = new AccessorGroup(StockGroupName.CurrencyFutures, base.Converter.ConvertList(_page.CurrencyGroup.Futures));			
		}

        protected override void MainMethod()
        {
            try
            {
				this.ReadOnce();
            }
            catch (Exception ex)
            {
				ExceptionLogger.LogException("Exception received while reading a Futures Page.", ex, false);
                base.OnErrorDetected();
            }
        }

		#endregion

	}
}
